/*
 * Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package pl.droidsonroids.gif.gifsample.slice;

import pl.droidsonroids.gif.gifsample.LogUtil;
import pl.droidsonroids.gif.gifsample.ResourceTable;

import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.components.ComponentContainer;
import ohos.agp.components.ComponentState;
import ohos.agp.components.LayoutScatter;
import ohos.agp.components.element.Element;
import ohos.agp.components.element.StateElement;
import ohos.global.resource.RawFileDescriptor;
import pl.droidsonroids.gif.GifDrawable;

import java.io.IOException;

/**
 * AnimatorSelectorSlice
 */
public class AnimatorSelectorSlice extends AbilitySlice {
    /**
     * TAG constant
     */
    public static final String TAG = "AnimatorSelectorSlice";
    ComponentContainer rootLayout;
    private CustomButton gifImgView = null;

    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        rootLayout = (ComponentContainer) LayoutScatter.getInstance(this)
                .parse(ResourceTable.Layout_animator_selector, null, false);
        gifImgView = (CustomButton)rootLayout.findComponentById(ResourceTable.Id_gifImgView);
        super.setUIContent(rootLayout);
    }

    @Override
    public void onActive() {
        super.onActive();
        gifImgView.setBackground(getJavaAnimatedBackground());
    }

    private Element getJavaAnimatedBackground() {
        RawFileDescriptor rawFileDescriptor = null;
        RawFileDescriptor rawFileDescriptor1 = null;
        GifDrawable gifDrawable = null;
        GifDrawable gifDrawable1 = null;
        try {
            rawFileDescriptor = getResourceManager()
                    .getRawFileEntry("entry/resources/rawfile/anim_flag_iceland.gif").openRawFileDescriptor();
            rawFileDescriptor1 = getResourceManager()
                    .getRawFileEntry("entry/resources/rawfile/anim_flag_hungary.gif").openRawFileDescriptor();
            gifDrawable = new GifDrawable(rawFileDescriptor);
            gifDrawable1 = new GifDrawable(rawFileDescriptor1);
        } catch (IOException e) {
            LogUtil.error(TAG, "IOException");
        }
        final StateElement stateListDrawable = new StateElement();
        stateListDrawable.addState(new int[]{ComponentState.COMPONENT_STATE_PRESSED}, gifDrawable);
        stateListDrawable.addState(new int[]{ComponentState.COMPONENT_STATE_EMPTY}, gifDrawable1);
        return stateListDrawable;
    }
}
